package com.asiainfo.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.asiainfo.bean.BusinessBean;
import com.asiainfo.bean.HelperBean;
import com.asiainfo.bean.MealBean;
import com.asiainfo.bean.PhoneBean;

import com.asiainfo.bean.UserSessionBean;
import com.asiainfo.dao.BaseDao;
import com.asiainfo.util.DBSequence;
import com.asiainfo.util.SessionInfoUtil;




@Controller
@RequestMapping("/phoneController")
public class PhoneController extends ModelAndView{
	
	private Logger logger = LogManager.getLogger(this.getClass());
	

	/**
	 * 数据操作封装DAO
	 */
	@Autowired
	private BaseDao dao;
	
	/**
	 * 方法描述：通话套餐使用操作
	 * 
	 */
	@RequestMapping(value = "/callUse.do")
	 
	public @ResponseBody
	JSONObject callUses(@RequestBody PhoneBean phoneBean, 
			HttpServletRequest request) throws Exception {
		JSONObject retMsg = new JSONObject();
		float mealCallCost=0;
		float businessCallCost=0;
		float totalCallCost=0;
		float billCost;
		int totalCall=0;
		int callUse=0;
		
		UserSessionBean userSessionBean = SessionInfoUtil.getUserSessBean(request.getSession());
		
		String userPhone = userSessionBean.getPhone();
		
		if(userPhone !=null){
			
			//获取话费余额
			PhoneBean  realTimeBalance = dao.getDAO().selectOne(
					"PhoneMapper.queryBalance", phoneBean.getPhone());
			float balance = Float.parseFloat(realTimeBalance.getReal_time_balance());
			
			PhoneBean currentUseCost  =	PhoneCurrentUseCost(phoneBean);
			//  每分钟话费
				float oneCallCost=Float.parseFloat(currentUseCost.getOne_call_cost());
			// 正在使用的通话费用
				float callUseCost=Float.parseFloat(currentUseCost.getCurrentCallCost());
			
			if(balance<callUseCost){//余额不足
				retMsg.put("isNotBalance", true);
			}else{//余额充足
				
				
				//判断套餐使用情况表是否有当前号码的记录
				int isMealUse = (Integer)this.dao.getDAO().selectOne(
						"PhoneMapper.queryMealUse", phoneBean);
				
				//插入或更新业务使用表
				if(isMealUse==0){
					
					phoneBean.setId(DBSequence.getInstance().getSequence());
					phoneBean.setMessage_use("0");
					phoneBean.setData_use("0");
					dao.getDAO().insert(
							"PhoneMapper.AddMealUse", phoneBean);	
				}else{
					 dao.getDAO().update(
							"PhoneMapper.updateAddCall", phoneBean);
				}
				
				
				
				//判断话费账单表是否有当前号码的记录
				int isMealUseCost = (Integer)this.dao.getDAO().selectOne(
						"PhoneMapper.queryMealUseCost", phoneBean);
				
				//判断是否使用套餐
				int isMeal = (Integer)this.dao.getDAO().selectOne(
						"HelperMapper.isMyMeal", phoneBean.getPhone());
				
				//判断是否使用业务
				int isBusiness = (Integer)this.dao.getDAO().selectOne(
						"HelperMapper.isMyBusinessCall", phoneBean.getPhone());
				
				if(isMeal > 1 ){
					retMsg.put("isNotOne", true);
					
				}else if(isMeal== 1||isBusiness!=0){
					//获取套餐通话总量
					HelperBean phoneTotal=PhoneTotal(phoneBean);
					totalCall=Integer.parseInt(phoneTotal.getTotalvoice());
					
					//通话总费用（套餐+业务）
					if(isMeal== 1){
						//套餐通话总费用
						MealBean myMealCost = dao.getDAO().selectOne(
								"HelperMapper.queryMyMealCost", phoneBean.getPhone());
						mealCallCost=Float.parseFloat(myMealCost.getCall_tariff());
					}
					if(isBusiness!=0){
						//业务通话总费用
						BusinessBean myBusinessCostCall = dao.getDAO().selectOne(
								"HelperMapper.queryMyBusinessCostCall", phoneBean.getPhone());
						businessCallCost=Float.parseFloat(myBusinessCostCall.getBusiness_cost());
					}
					
					
				
		/*下面就是有套餐或业务的情况，根据套餐总量与总费用判断  账单表什么时候该插入或更新什么数据*/
				
				   totalCallCost=businessCallCost+mealCallCost;  //通话总费用（套餐+业务）
				   
				 //获取套餐通话使用量
					HelperBean phoneUse=PhoneUse(phoneBean);
					callUse=Integer.parseInt(phoneUse.getCall_use());
				 
			        
					
			if(isMealUseCost==0){//话费账单表无该号码  插入数据
				float add_call=Integer.parseInt(phoneBean.getAdd_call());
				if(add_call<totalCall&&callUseCost<totalCallCost){  //使用的套餐量<总套餐量，所用费用<套餐总通话费用，正常插入数据
					phoneBean.setId_cost(DBSequence.getInstance().getSequence());
					phoneBean.setAdd_message("0");
					phoneBean.setAdd_data("0");
					phoneBean.setOther_business_cost("0");
					dao.getDAO().insert(
							"PhoneMapper.AddMealUseCost", phoneBean);	
					updatPhoneBalance(phoneBean,callUseCost);//更新余额表
				}else if(add_call<totalCall&&callUseCost>=totalCallCost){//使用的套餐量<总套餐量，所用费用>=套餐总通话费用，插入的数据为套餐总通话费用
					//插入价格为套餐费用
					phoneBean.setId_cost(DBSequence.getInstance().getSequence());
					phoneBean.setCall_cost(String.valueOf(totalCallCost));
					phoneBean.setMessage_cost("0");
					phoneBean.setData_cost("0");
					phoneBean.setOther_business_cost("0");
					dao.getDAO().insert(
							"PhoneMapper.insertMealUseCost", phoneBean);	
					updatPhoneBalance(phoneBean,callUseCost);//更新余额表
				}else if(add_call>=totalCall){//使用的套餐量>=总套餐量,插入数据为套餐费用+多出的费用
					//插入价格为套餐费用+多出的费用
					phoneBean.setId_cost(DBSequence.getInstance().getSequence());
					phoneBean.setCall_cost(String.valueOf(totalCallCost+(add_call-totalCall)*oneCallCost));
					phoneBean.setMessage_cost("0");
					phoneBean.setData_cost("0");
					phoneBean.setOther_business_cost("0");
					dao.getDAO().insert(
							"PhoneMapper.insertMealUseCost", phoneBean);	
					updatPhoneBalance(phoneBean,callUseCost);//更新余额表
				}
				
			//话费账单表有该号码  更新数据
			}else{
				//获取已有的通话账单
				PhoneBean billCostBean = PhoneBillCost(phoneBean);
				billCost= Float.parseFloat(billCostBean.getCall_cost());
				
				if(callUse<totalCall&&(callUseCost+billCost)<=totalCallCost){//使用的套餐量<总套餐量，所用费用<套餐总通话费用，正常更新数据
					dao.getDAO().update(
							"PhoneMapper.updateCallCost", phoneBean);
					updatPhoneBalance(phoneBean,callUseCost);//更新余额表
				}else if(callUse<=totalCall&&(callUseCost+billCost)>totalCallCost){//使用的套餐量<总套餐量，所用费用>=套餐总通话费用，更新的数据为套餐总通话费用
					phoneBean.setCall_cost(String.valueOf(totalCallCost));
					dao.getDAO().update(
							"PhoneMapper.updateCallCost2", phoneBean);
					updatPhoneBalance(phoneBean,callUseCost);//更新余额表
				}else if(callUse>totalCall){//使用的套餐量>=总套餐量,正常更新数据
					phoneBean.setCall_cost(String.valueOf(totalCallCost+(callUse-totalCall)*oneCallCost));
					
					dao.getDAO().update(
							"PhoneMapper.updateCallCost2", phoneBean);
					updatPhoneBalance(phoneBean,callUseCost);//更新余额表
				}
			}
			retMsg.put("flag", true);
				}
				
				/*下面就是无套餐无业务的情况，  账单表插入或更新什么数据*/
				
				else{//无套餐无话费的情况
					
					if(isMealUseCost==0){//话费账单表无该号码  插入数据
						phoneBean.setId_cost(DBSequence.getInstance().getSequence());
						phoneBean.setAdd_message("0");
						phoneBean.setAdd_data("0");
						phoneBean.setOther_business_cost("0");
						dao.getDAO().insert(
								"PhoneMapper.AddMealUseCost", phoneBean);	
						updatPhoneBalance(phoneBean,callUseCost);//更新余额表
					}else{//话费账单表有该号码   更新
							dao.getDAO().update(
									"PhoneMapper.updateCallCost", phoneBean);
							updatPhoneBalance(phoneBean,callUseCost);//更新余额表
							}
					retMsg.put("flag", true);
				}
			
			}	
				
	}
		else{
			retMsg.put("flag", false);
		}
		
		return retMsg;
	}
	
	
	
	
	/**
	 * 方法描述：短信套餐使用操作
	 * 
	 */
	@RequestMapping(value = "/messageUse.do")
	 
	public @ResponseBody
	JSONObject messageUses(@RequestBody PhoneBean phoneBean, 
			HttpServletRequest request) throws Exception {
		JSONObject retMsg = new JSONObject();
		float mealMessageCost=0;
		float businessMessageCost=0;
		float totalMessageCost=0;
		float billCost;
		int totalMessage=0;
		int messageUse=0;
		
		UserSessionBean userSessionBean = SessionInfoUtil.getUserSessBean(request.getSession());
		
		String userPhone = userSessionBean.getPhone();
		
		if(userPhone !=null){
			
			//获取话费余额
			PhoneBean  realTimeBalance = dao.getDAO().selectOne(
					"PhoneMapper.queryBalance", phoneBean.getPhone());
			float balance = Float.parseFloat(realTimeBalance.getReal_time_balance());
			
			PhoneBean currentUseCost  =	PhoneCurrentUseCost(phoneBean);
			//  每条短信费
				float oneMessageCost=Float.parseFloat(currentUseCost.getOne_message_cost());
			// 正在使用的短信费用
				float messageUseCost=Float.parseFloat(currentUseCost.getCurrentMessageCost());
			     
				
			
			if(balance<messageUseCost){//余额不足
				retMsg.put("isNotBalance", true);
			}else{//余额充足
			
			
			//判断套餐使用情况表是否有当前号码的记录
			int isMealUse = (Integer)this.dao.getDAO().selectOne(
					"PhoneMapper.queryMealUse", phoneBean);
			
			if(isMealUse==0){
				
				phoneBean.setId(DBSequence.getInstance().getSequence());
				phoneBean.setCall_use("0");
				phoneBean.setData_use("0");
				dao.getDAO().insert(
						"PhoneMapper.AddMealUse", phoneBean);	
			}else{
				 dao.getDAO().update(
						"PhoneMapper.updateAddMessage", phoneBean);
			}
			
			
			
			//判断话费账单表是否有当前号码的记录
			int isMealUseCost = (Integer)this.dao.getDAO().selectOne(
					"PhoneMapper.queryMealUseCost", phoneBean);
			
			//判断是否使用套餐
			int isMeal = (Integer)this.dao.getDAO().selectOne(
					"HelperMapper.isMyMeal", phoneBean.getPhone());
			
			//判断是否使用业务
			int isBusiness = (Integer)this.dao.getDAO().selectOne(
					"HelperMapper.isMyBusinessMessage", phoneBean.getPhone());
			
			if(isMeal > 1 ){
				retMsg.put("isNotOne", true);
				
			}else if(isMeal== 1||isBusiness!=0){
				//获取套餐短信总量
				HelperBean phoneTotal=PhoneTotal(phoneBean);
				totalMessage=Integer.parseInt(phoneTotal.getTotalmessage());
				
				//短信总费用（套餐+业务）
				if(isMeal== 1){
					//套餐短信总费用
					MealBean myMealCost = dao.getDAO().selectOne(
							"HelperMapper.queryMyMealCost", phoneBean.getPhone());
					mealMessageCost=Float.parseFloat(myMealCost.getMessage_tariff());
				}
				if(isBusiness!=0){
					//业务短信总费用
					BusinessBean myBusinessCostCall = dao.getDAO().selectOne(
							"HelperMapper.queryMyBusinessCostMessage", phoneBean.getPhone());
					businessMessageCost=Float.parseFloat(myBusinessCostCall.getBusiness_cost());
				}
				
				
			
	/*下面就是有套餐或业务的情况，根据套餐总量与总费用判断  账单表什么时候该插入或更新什么数据*/
			
			   totalMessageCost=businessMessageCost+mealMessageCost;  //短信总费用（套餐+业务）
			   
			 //获取套餐短信使用量
				HelperBean phoneUse=PhoneUse(phoneBean);
				messageUse=Integer.parseInt(phoneUse.getMessage_use());
			 
		     	
		if(isMealUseCost==0){//话费账单表无该号码  插入数据
			float add_message=Integer.parseInt(phoneBean.getAdd_message());
			
			if(add_message<totalMessage && messageUseCost<totalMessageCost){  //使用的套餐量<总套餐量，所用费用<套餐总通话费用，正常插入数据
				phoneBean.setId_cost(DBSequence.getInstance().getSequence());
				phoneBean.setAdd_call("0");
				phoneBean.setAdd_data("0");
				phoneBean.setOther_business_cost("0");
				dao.getDAO().insert(
						"PhoneMapper.AddMealUseCost", phoneBean);	
				updatPhoneBalance(phoneBean,messageUseCost);//更新余额表
			}else if(add_message<totalMessage && messageUseCost>=totalMessageCost){//使用的套餐量<总套餐量，所用费用>=套餐总通话费用，插入的数据为套餐总通话费用
				//插入价格为套餐费用
				phoneBean.setId_cost(DBSequence.getInstance().getSequence());
				phoneBean.setCall_cost("0");
				phoneBean.setMessage_cost(String.valueOf(totalMessageCost));
				phoneBean.setData_cost("0");
				phoneBean.setOther_business_cost("0");
				dao.getDAO().insert(
						"PhoneMapper.insertMealUseCost", phoneBean);	
				updatPhoneBalance(phoneBean,messageUseCost);//更新余额表
			}else if(add_message>=totalMessage){//使用的套餐量>=总套餐量,插入数据为套餐费用+多出的费用
				//插入价格为套餐费用+多出的费用
				phoneBean.setId_cost(DBSequence.getInstance().getSequence());
				phoneBean.setCall_cost("0");
				phoneBean.setMessage_cost(String.valueOf(totalMessageCost+(add_message-totalMessage)*oneMessageCost));
				phoneBean.setData_cost("0");
				phoneBean.setOther_business_cost("0");
				dao.getDAO().insert(
						"PhoneMapper.insertMealUseCost", phoneBean);	
				updatPhoneBalance(phoneBean,messageUseCost);//更新余额表
			}
			
		//话费账单表有该号码  更新数据
		}else{
			//获取已有的短信账单
			PhoneBean billCostBean = PhoneBillCost(phoneBean);
			billCost= Float.parseFloat(billCostBean.getMessage_cost());
			
			if(messageUse<totalMessage&&(messageUseCost+billCost)<=totalMessageCost){//使用的套餐量<总套餐量，所用费用<套餐总通话费用，正常更新数据
				dao.getDAO().update(
						"PhoneMapper.updateMessageCost", phoneBean);
				updatPhoneBalance(phoneBean,messageUseCost);//更新余额表
			}else if(messageUse<=totalMessage&&(messageUseCost+billCost)>totalMessageCost){//使用的套餐量<总套餐量，所用费用>=套餐总通话费用，更新的数据为套餐总通话费用
				phoneBean.setMessage_cost(String.valueOf(totalMessageCost));
				dao.getDAO().update(
						"PhoneMapper.updateMessageCost2", phoneBean);
				updatPhoneBalance(phoneBean,messageUseCost);//更新余额表
			}else if(messageUse>totalMessage){//使用的套餐量>=总套餐量,正常更新数据
				phoneBean.setMessage_cost(String.valueOf(totalMessageCost+(messageUse-totalMessage)*oneMessageCost));
				
				dao.getDAO().update(
						"PhoneMapper.updateMessageCost2", phoneBean);
				updatPhoneBalance(phoneBean,messageUseCost);//更新余额表
			}
			
		}
		
		retMsg.put("flag", true);
			}

			/*下面就是无套餐无业务的情况，  账单表插入或更新什么数据*/
			
			else{//无套餐无话费的情况
				
				if(isMealUseCost==0){//话费账单表无该号码  插入数据
					phoneBean.setId_cost(DBSequence.getInstance().getSequence());
					phoneBean.setAdd_call("0");
					phoneBean.setAdd_data("0");
					phoneBean.setOther_business_cost("0");
					dao.getDAO().insert(
							"PhoneMapper.AddMealUseCost", phoneBean);
					updatPhoneBalance(phoneBean,messageUseCost);//更新余额表
				}else{//话费账单表有该号码   更新
						dao.getDAO().update(
								"PhoneMapper.updateMessageCost", phoneBean);
						updatPhoneBalance(phoneBean,messageUseCost);//更新余额表
						}
				retMsg.put("flag", true);
			}
		 }
		}else{
			retMsg.put("flag", false);
		}
		
		return retMsg;
	}
	
	
	
	
	/**
	 * 方法描述：流量套餐使用操作
	 * 
	 */
	@RequestMapping(value = "/dataUse.do")
	 
	public @ResponseBody
	JSONObject dataUses(@RequestBody PhoneBean phoneBean, 
			HttpServletRequest request) throws Exception {
		JSONObject retMsg = new JSONObject();
		float mealDataCost=0;
		float businessDataCost=0;
		float totalDataCost=0;
		float billCost;
		int totalData=0;
		int dataUse=0;
		
		UserSessionBean userSessionBean = SessionInfoUtil.getUserSessBean(request.getSession());
		
		String userPhone = userSessionBean.getPhone();
		
		if(userPhone !=null){
			
			//获取话费余额
			PhoneBean  realTimeBalance = dao.getDAO().selectOne(
					"PhoneMapper.queryBalance", phoneBean.getPhone());
			float balance = Float.parseFloat(realTimeBalance.getReal_time_balance());
			

			PhoneBean currentUseCost  =	PhoneCurrentUseCost(phoneBean);
		//  每M 的流量费用
			float oneDataCost=Float.parseFloat(currentUseCost.getOne_data_cost());
		// 正在使用的流量费用
			float dataUseCost=Float.parseFloat(currentUseCost.getCurrentDataCost());
		     
			
			if(balance<dataUseCost){//余额不足
				retMsg.put("isNotBalance", true);
			}else{//余额充足
			
			
			//判断套餐使用情况表是否有当前号码的记录
			int isMealUse = (Integer)this.dao.getDAO().selectOne(
					"PhoneMapper.queryMealUse", phoneBean);
			
			if(isMealUse==0){
				
				phoneBean.setId(DBSequence.getInstance().getSequence());
				phoneBean.setMessage_use("0");
				phoneBean.setCall_use("0");
				dao.getDAO().insert(
						"PhoneMapper.AddMealUse", phoneBean);	
			}else{
				 dao.getDAO().update(
						"PhoneMapper.updateAddData", phoneBean);
			}
			
			
			
			//判断话费账单表是否有当前号码的记录
			int isMealUseCost = (Integer)this.dao.getDAO().selectOne(
					"PhoneMapper.queryMealUseCost", phoneBean);
			
			//判断是否使用套餐
			int isMeal = (Integer)this.dao.getDAO().selectOne(
					"HelperMapper.isMyMeal", phoneBean.getPhone());
			
			//判断是否使用业务
			int isBusiness = (Integer)this.dao.getDAO().selectOne(
					"HelperMapper.isMyBusinessData", phoneBean.getPhone());
			
			if(isMeal > 1 ){
				retMsg.put("isNotOne", true);
				
			}else if(isMeal== 1||isBusiness!=0){
				//获取套餐流量总量
				HelperBean phoneTotal=PhoneTotal(phoneBean);
				totalData=Integer.parseInt(phoneTotal.getTotaldata());
				
				//流量总费用（套餐+业务）
				if(isMeal== 1){
					//套餐流量总费用
					MealBean myMealCost = dao.getDAO().selectOne(
							"HelperMapper.queryMyMealCost", phoneBean.getPhone());
					mealDataCost=Float.parseFloat(myMealCost.getData_tariff());
				}
				if(isBusiness!=0){
					//业务流量总费用
					BusinessBean myBusinessCostCall = dao.getDAO().selectOne(
							"HelperMapper.queryMyBusinessCostData", phoneBean.getPhone());
					businessDataCost=Float.parseFloat(myBusinessCostCall.getBusiness_cost());
				}
				
				
			
	/*下面就是有套餐或业务的情况，根据套餐总量与总费用判断  账单表什么时候该插入或更新什么数据*/
			
			   totalDataCost=businessDataCost+mealDataCost;  //流量总费用（套餐+业务）
			   
			 //获取套餐流量使用量
				HelperBean phoneUse=PhoneUse(phoneBean);
				dataUse=Integer.parseInt(phoneUse.getData_use());
			 
		     
				

		if(isMealUseCost==0){//话费账单表无该号码  插入数据
			float add_data=Integer.parseInt(phoneBean.getAdd_data());
			if(add_data<totalData && dataUseCost<totalDataCost){  //使用的套餐量<总套餐量，所用费用<套餐总通话费用，正常插入数据
				phoneBean.setId_cost(DBSequence.getInstance().getSequence());
				phoneBean.setAdd_message("0");
				phoneBean.setAdd_call("0");
				phoneBean.setOther_business_cost("0");
				dao.getDAO().insert(
						"PhoneMapper.AddMealUseCost", phoneBean);
				updatPhoneBalance(phoneBean,dataUseCost);//更新余额表
			}else if(add_data<totalData && dataUseCost>=totalDataCost){//使用的套餐量<总套餐量，所用费用>=套餐总通话费用，插入的数据为套餐总通话费用
				//插入价格为套餐费用
				phoneBean.setId_cost(DBSequence.getInstance().getSequence());
				phoneBean.setData_cost(String.valueOf(totalDataCost));
				phoneBean.setMessage_cost("0");
				phoneBean.setCall_cost("0");
				phoneBean.setOther_business_cost("0");
				dao.getDAO().insert(
						"PhoneMapper.insertMealUseCost", phoneBean);	
				updatPhoneBalance(phoneBean,dataUseCost);//更新余额表
			}else if(add_data>=totalData){//使用的套餐量>=总套餐量,插入数据为套餐费用+多出的费用
				//插入价格为套餐费用+多出的费用
				phoneBean.setId_cost(DBSequence.getInstance().getSequence());
				phoneBean.setData_cost(String.valueOf(totalDataCost+(add_data-totalData)*oneDataCost));
				phoneBean.setMessage_cost("0");
				phoneBean.setCall_cost("0");
				phoneBean.setOther_business_cost("0");
				dao.getDAO().insert(
						"PhoneMapper.insertMealUseCost", phoneBean);	
				updatPhoneBalance(phoneBean,dataUseCost);//更新余额表
			}
			
		//话费账单表有该号码  更新数据
		}else{
			//获取已有的流量账单
			PhoneBean billCostBean = PhoneBillCost(phoneBean);
			billCost= Float.parseFloat(billCostBean.getData_cost());
			
			if(dataUse<totalData&&(dataUseCost+billCost)<=totalDataCost){//使用的套餐量<总套餐量，所用费用<套餐总通话费用，正常更新数据
				dao.getDAO().update(
						"PhoneMapper.updateDataCost", phoneBean);
				updatPhoneBalance(phoneBean,dataUseCost);//更新余额表
			}else if(dataUse<=totalData&&(dataUseCost+billCost)>totalDataCost){//使用的套餐量<总套餐量，所用费用>=套餐总通话费用，更新的数据为套餐总通话费用
				phoneBean.setData_cost(String.valueOf(totalDataCost));
				dao.getDAO().update(
						"PhoneMapper.updateDataCost2", phoneBean);
				updatPhoneBalance(phoneBean,dataUseCost);//更新余额表
			}else if(dataUse>totalData){//使用的套餐量>=总套餐量,正常更新数据
				phoneBean.setData_cost(String.valueOf(totalDataCost+(dataUse-totalData)*oneDataCost));
				
				dao.getDAO().update(
						"PhoneMapper.updateDataCost2", phoneBean);
				updatPhoneBalance(phoneBean,dataUseCost);//更新余额表
			}
			
		}
		
		retMsg.put("flag", true);
			}

			/*下面就是无套餐无业务的情况，  账单表插入或更新什么数据*/
			
			else{//无套餐无业务的情况
				
				if(isMealUseCost==0){//话费账单表无该号码  插入数据
					phoneBean.setId_cost(DBSequence.getInstance().getSequence());
					phoneBean.setAdd_message("0");
					phoneBean.setAdd_call("0");
					phoneBean.setOther_business_cost("0");
					dao.getDAO().insert(
							"PhoneMapper.AddMealUseCost", phoneBean);	
					updatPhoneBalance(phoneBean,dataUseCost);//更新余额表
				}else{//话费账单表有该号码   更新
						dao.getDAO().update(
								"PhoneMapper.updateDataCost", phoneBean);
						updatPhoneBalance(phoneBean,dataUseCost);//更新余额表
						}
				retMsg.put("flag", true);
			}
		 }
		}else{
			retMsg.put("flag", false);
		}
		
		return retMsg;
	}
	
	
	
	
	
	
	
	
	/**
	 * 方法描述：获取套餐总量
	 * 
	 */
	public HelperBean PhoneTotal(PhoneBean phoneBean){
		
		HelperBean helperBean= new HelperBean();
		//套餐总量
		int mealcall = 0;
		int mealmessage = 0;
		int mealdata = 0;
				
		//业务总量
		int businesscall = 0;
		int businessmessage = 0;
		int businessdata = 0;
		
		//获得套餐总量
		HelperBean totalMeal = dao.getDAO().selectOne(
				"HelperMapper.queryTotalMeal", phoneBean.getPhone());
		
		mealcall = Integer.parseInt(totalMeal.getTotalvoice());
		mealmessage = Integer.parseInt(totalMeal.getTotalmessage());
		mealdata = Integer.parseInt(totalMeal.getTotaldata());
	
		
		//获得业务总量
		HelperBean totalBusiness = dao.getDAO().selectOne(
				"HelperMapper.queryTotalBusiness", phoneBean.getPhone());
		
		businesscall = Integer.parseInt(totalBusiness.getBustotalvoice());
		 businessmessage = Integer.parseInt(totalBusiness.getBustotalmessage());
		 businessdata = Integer.parseInt(totalBusiness.getBustotaldata());
		 
		 helperBean.setTotaldata(String.valueOf(businessdata + mealdata));
		 helperBean.setTotalmessage(String.valueOf(mealmessage + businessmessage));
		 helperBean.setTotalvoice(String.valueOf(mealcall + businesscall));
		
		return helperBean;}
	
	/**
	 * 方法描述：获取套餐使用量
	 * 
	 */
     public HelperBean PhoneUse(PhoneBean phoneBean){
		
		HelperBean helperBean=new HelperBean();
		
		 
		//获取套餐使用量
			HelperBean mealAllowanceInfo = dao.getDAO().selectOne(
					"HelperMapper.queryMealAllowance", phoneBean.getPhone());
		 
		 helperBean.setCall_use(mealAllowanceInfo.getCall_use());
		 helperBean.setData_use(mealAllowanceInfo.getData_use());
		 helperBean.setMessage_use(mealAllowanceInfo.getMessage_use());
		 
		return helperBean;}
     
     
     
     /**
 	 * 方法描述：获取当前使用费用
 	 * 
 	 */
      
	public PhoneBean PhoneCurrentUseCost(PhoneBean phoneBean){
 		
    	  PhoneBean currentUseCostBean=new PhoneBean();
 		
 		 
 		//查询套餐资费/分钟/M/条
 				PhoneBean mealOneCost = dao.getDAO().selectOne(
 							"PhoneMapper.queryMealOneCost");
 				//正在消费的费用
 				
 				if(phoneBean.getAdd_call()!=null){
 					 float callUseCost=Float.parseFloat(mealOneCost.getOne_call_cost())
 	 						 *Float.parseFloat(phoneBean.getAdd_call());
 					 
 					currentUseCostBean.setCurrentCallCost(String.valueOf(callUseCost));
 				}
 				
 				if (phoneBean.getAdd_data()!=null) {
 					float dataUseCost=Float.parseFloat(mealOneCost.getOne_data_cost())
 							 *Float.parseFloat(phoneBean.getAdd_data());
 					
 					currentUseCostBean.setCurrentDataCost(String.valueOf(dataUseCost));
				} 
 				
 				if(phoneBean.getAdd_message()!=null){
 					float messageUseCost=Float.parseFloat(mealOneCost.getOne_message_cost())
 							 *Float.parseFloat(phoneBean.getAdd_message());
 					
 					currentUseCostBean.setCurrentMessageCost(String.valueOf(messageUseCost));
 				}
 				
 		  
 				currentUseCostBean.setOne_call_cost(mealOneCost.getOne_call_cost());
 				currentUseCostBean.setOne_data_cost(mealOneCost.getOne_data_cost());
 				currentUseCostBean.setOne_message_cost(mealOneCost.getOne_message_cost());
 				
 		return currentUseCostBean;}
     
      /**
   	 * 方法描述：获取账单费用
   	 * 
   	 */
        
  	public PhoneBean PhoneBillCost(PhoneBean phoneBean){
   		
      	  PhoneBean billCostBean=new PhoneBean();
   		
   				PhoneBean billCost = dao.getDAO().selectOne(
   							"PhoneMapper.querybillCost",phoneBean.getPhone());
   				
   					billCostBean.setCall_cost(billCost.getCall_cost());
   	   				billCostBean.setData_cost(billCost.getData_cost());
   	   				billCostBean.setMessage_cost(billCost.getMessage_cost());
   	   				billCostBean.setOther_business_cost(billCost.getOther_business_cost());
   	   				billCostBean.setBilltotal(billCost.getBilltotal());
   	   		      
   				
   				return billCostBean;}
       
  	
  	/**
   	 * 方法描述：更新话费余额
   	 * 
   	 */
        
  	public void updatPhoneBalance(PhoneBean phoneBean,float currentCost){
   		
      	phoneBean.setCurrentCost(String.valueOf(currentCost));
      	dao.getDAO().update(
				"PhoneMapper.updateBalance", phoneBean);      
   		}

}
  